﻿using System;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.IO;
using System.Runtime.CompilerServices;
using System.Windows.Controls;
using System.Windows.Input;
using Red.Controls.Diagrams.ViewModels;
using Red.Controls.Shell;

namespace Red.Toolkit.AITreeEditor
{
    [Export( typeof( IEditorInfo ) )]
    public class EditorInfo : IEditorInfo
    {
        public string Name => "AI Tree Editor";
        public Type WindowClass => typeof( AITreeEditor );
        public Type SettingsClass => typeof( UserSettings );
    }

    /// <summary>
    /// Interaction logic for AITreeEditor.xaml
    /// </summary>
    public partial class AITreeEditor : RedEditorWindow, INotifyPropertyChanged
    {
        #region C-tor

        public AITreeEditor( IEditorHost host, EditorInstanceData instanceData, EditorSharedData sharedData )
            : base( host, instanceData, sharedData )
        {
            InitializeComponent();
            LoadDeclarators();
            BackendInitialized += AITreeEditor_BackendInitialized;
        }

        #endregion C-tor

        #region Event handlers

        private async void AITreeEditor_BackendInitialized( object sender, EventArgs e )
        {
            await _nodeControl.LoadAsync( InstanceData.RootEditorItem.Data );
        }

        private void NodeControl_SelectionChanged( object sender, SelectionChangedEventArgs e )
        {
            if ( e.AddedItems.Count == 1 )
            {
                // selected one node
                var selectedNodeViewModel = e.AddedItems[ 0 ] as NodeViewModel;
                var iObject = selectedNodeViewModel.Object;
                MainDataCategory.DataContext = iObject.GetDataItem();

                // TODO: this probably will not be needed after we preserve expanded state despite changing DataContext
                MainDataCategory.IsExpanded = true;
            }            
        }

        private void Cut_CanExecute( object sender, CanExecuteRoutedEventArgs e )
        {
            e.CanExecute = _nodeControl.CanCopySelectedNodes();
        }

        private void Cut_Executed( object sender, ExecutedRoutedEventArgs e )
        {
            _nodeControl.CutSelectedNodes();
        }

        private void Copy_CanExecute( object sender, CanExecuteRoutedEventArgs e )
        {
            e.CanExecute = _nodeControl.CanCopySelectedNodes();
            e.Handled = true;
        }

        private void Copy_Executed( object sender, ExecutedRoutedEventArgs e )
        {
            _nodeControl.CopySelectedNodes();
        }

        private void Paste_CanExecute( object sender, CanExecuteRoutedEventArgs e )
        {
            e.CanExecute = _nodeControl.CanPasteNodes();
        }

        private void Paste_Executed( object sender, ExecutedRoutedEventArgs e )
        {
            _nodeControl.PaseteNodes();
        }

        #endregion Event handlers

        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged( [CallerMemberName] string propertyName = null )
        {
            PropertyChanged?.Invoke( this, new PropertyChangedEventArgs( propertyName ) );
        }

        #endregion INotifyPropertyChanged

        #region Private methods

        private void LoadDeclarators()
        {
            var engineRootPath = Path.Combine( SharedData.DepotInfo.RootPath, SharedData.EnginePath );
            var filePath = Path.Combine( engineRootPath, @"nodedeclarators\ai.dcl" );
            _nodeControl.DeclaratorsPath = filePath;
        }

        #endregion Private methods
    }
}
